<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 表单区域 -->
      <el-form ref="loginFromRefs" :model = "loginFrom" :rules = "loginFromRules">
          <!-- 用户名和密码输入区域 -->
        <el-form-item prop = "username">
          <el-input v-model = "loginFrom.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
          <el-form-item prop = "password">
          <el-input v-model = "loginFrom.password"  type="password" prefix-icon="iconfont icon-password"></el-input>
        </el-form-item>
         <!-- 提交和重置按钮区域 -->
        <el-form-item class="btns">
            <el-button type="primary" @click ="login">确定</el-button>
            <el-button type="info" @click = "resetLoginFrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data(){
      return{
          // 用户名密码数据
           loginFrom:{
               username:'admin',
               password:'123456'
           },
           //用户名密码验证规则数据
           loginFromRules:{
               username:[
                  { required: true, message: '请输入用户名', trigger: 'blur' },
                  { min: 3, max: 9, message: '长度在  3 到 9 个字符', trigger: 'blur' }
               ],
               password:[
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 9, message: '长度在 6 到 9 个字符', trigger: 'blur'}
               ]
           }
      }
  },

  methods:{
       //重置表单数据
    resetLoginFrom(){
       console.log(this);
       this.$refs.loginFromRefs.resetFields();
    },
    login(){
      this.$refs.loginFromRefs.validate(async(valid) => {
            if(!valid){
               return 
            }
           const {data:res} = await this.$http.post('login',this.loginFrom);
           　　console.log(res);
              if(res.meta.status != 200) return this.$message.error("登录失败"); 
               this.$message.success("登录成功");
                window.sessionStorage.setItem('token',res.data.token);
                this.$router.push("/home");
               
                
      })
    }

  }
};
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
  background-color: #2b4b6b;

  .login_box {
    position: absolute;
    width: 450px;
    height: 300px;
    left: 50%;
    top: 50%;
    background-color: #fff;
    border-radius: 3px;
    transform: translate(-50%, -50%);

    .avatar {
      position: absolute;
      left: 50%;
      width: 130px;
      height: 130px;
      padding: 10px;
      border: 1px solid #ddd;
      border-radius: 50%;
      background-color: #fff;
      box-shadow: 0 0 10px #ddd;
      transform: translate(-50%, -50%);

      img {
        width: 100%;
        height: 100%;
        border: 1px solid #999;
        border-radius: 50%;
        background-color: #999;
      }
    }
  }
}
.el-form{
    padding: 0 50px;
    margin-top: 100px;

    .btns{
        display: flex;
        justify-content: flex-end;
    }

}
</style>